package org.jxc.web.action;

import java.util.List;

import org.jxc.constants.JXCString;
import org.jxc.domain.Product;
import org.jxc.domain.ProductType;
import org.jxc.domain.PurchaseOrder;
import org.jxc.domain.PurchaseOrderProduct;
import org.jxc.service.PurchaseOrderProductService;
import org.jxc.service.PurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.util.LoginBean;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class PurchaseOrderAction extends GenericAction<PurchaseOrder> {

	private static final long serialVersionUID = -4867867482994497216L;

	@Autowired
	private PurchaseOrderService productOrderService;
	@Autowired
	private PurchaseOrderProductService purchaseOrderProductService;
	private List<ProductType> productTypeList;
	private List<PurchaseOrder> purchaseOrderList;
	private List<PurchaseOrderProduct> itemList;
	private List<Product> productList;
	private PurchaseOrder purchaseOrder;
	private String purchaseOrderId;
	private String productTypeId;
	private Integer auditState;

	public String listPagination() throws Exception {
		page = productOrderService.listPagination(page, productTypeId);
		return SUCCESS;
	}

	/**
	 * 预新建
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toSave() throws Exception {
		return SUCCESS;
	}

	/**
	 * 新建
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		LoginBean bean = LoginBean.getLoginBean();
		purchaseOrder.setAuditor(bean.getUser().getDepartment().getManager());
		double total = 0.0;
		if (itemList != null) {
			for (PurchaseOrderProduct pop : itemList) {
				total += pop.getLastPrice();
			}
		}
		purchaseOrder.setTotalPrice(total);
		productOrderService.savePurchaseOrder(purchaseOrder);
		if (itemList != null) {
			for (PurchaseOrderProduct pop : itemList) {
				pop.setPurchaseOrder(purchaseOrder);
				purchaseOrderProductService.savePurchaseOrderProduct(pop);
			}
		}
		return SUCCESS;
	}

	/**
	 * 预修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toUpdate() throws Exception {
		purchaseOrder = productOrderService.findById(purchaseOrderId);
		itemList = purchaseOrderProductService
				.findAllPurchaseOrderProductByPurchaseOrderId(purchaseOrderId);
		return SUCCESS;
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		double total = 0.0;
		purchaseOrderProductService
				.deleteProductOrderProductByProductOrderId(purchaseOrder
						.getId());
		if (itemList != null) {
			for (PurchaseOrderProduct pop : itemList) {
				total += pop.getLastPrice();
				pop.setPurchaseOrder(purchaseOrder);
				purchaseOrderProductService.savePurchaseOrderProduct(pop);
			}
		}
		purchaseOrder.setTotalPrice(total);
		productOrderService.updatePurchaseOrder(purchaseOrder);
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		for (String str : idList) {
			try {
				productOrderService.deleteProductOrder(str);
			} catch (Exception e) {
				continue;
			}
		}
		return SUCCESS;
	}

	public String apply() throws Exception {
		purchaseOrder = productOrderService.findById(purchaseOrderId);
		purchaseOrder.setAuditState(JXCString.APPLY);
		productOrderService.updatePurchaseOrder(purchaseOrder);
		return SUCCESS;
	}

	public String audit() throws Exception {
		purchaseOrder = productOrderService.findById(purchaseOrderId);
		purchaseOrder.setAuditState(auditState);
		productOrderService.updatePurchaseOrder(purchaseOrder);
		return SUCCESS;
	}

	public List<ProductType> getProductTypeList() {
		return productTypeList;
	}

	public String toView() throws Exception {
		purchaseOrder = productOrderService.findById(purchaseOrderId);
		itemList = purchaseOrderProductService
				.findAllPurchaseOrderProductByPurchaseOrderId(purchaseOrderId);
		return SUCCESS;
	}

	public void setProductTypeList(List<ProductType> productTypeList) {
		this.productTypeList = productTypeList;
	}

	public String getProductTypeId() {
		return productTypeId;
	}

	public void setProductTypeId(String productTypeId) {
		this.productTypeId = productTypeId;
	}

	public List<PurchaseOrder> getPurchaseOrderList() {
		return purchaseOrderList;
	}

	public void setPurchaseOrderList(List<PurchaseOrder> purchaseOrderList) {
		this.purchaseOrderList = purchaseOrderList;
	}

	public List<PurchaseOrderProduct> getItemList() {
		return itemList;
	}

	public void setItemList(List<PurchaseOrderProduct> itemList) {
		this.itemList = itemList;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public PurchaseOrder getPurchaseOrder() {
		return purchaseOrder;
	}

	public void setPurchaseOrder(PurchaseOrder purchaseOrder) {
		this.purchaseOrder = purchaseOrder;
	}

	public String getPurchaseOrderId() {
		return purchaseOrderId;
	}

	public void setPurchaseOrderId(String purchaseOrderId) {
		this.purchaseOrderId = purchaseOrderId;
	}

	public Integer getAuditState() {
		return auditState;
	}

	public void setAuditState(Integer auditState) {
		this.auditState = auditState;
	}
}